import D2Ext

from .CResource import CResource
from .CWidget import CWidget


class CImage(CWidget):

    def __init__(self, parent=None):
        super(CImage, self).__init__(parent)
        # self.image_file = D2Ext.Image_LoadFromFile(image_file)
        self.image_file = None
        self.frame = 0
        self.color = 0
        self.set_frame(0)

    def set_image(self, image_file):
        self.image_file = CResource.get_image(image_file)
        D2Ext.LogMsg("set_image, self.image_file=%s" % self.image_file)
        # self.image_file = D2Ext.Image_LoadFromFile(image_file)
        self.width = D2Ext.Image_GetWidth(self.image_file, self.frame)
        self.height = D2Ext.Image_GetHeight(self.image_file, self.frame)

    def set_frame(self, frame):
        self.frame = frame
        if self.image_file:
            D2Ext.LogMsg("set_frame:obj=%s,frame=%s" % (self, frame))

            D2Ext.Image_SetFrame(self.image_file, self.frame)
            self.width = D2Ext.Image_GetWidth(self.image_file, self.frame)
            self.height = D2Ext.Image_GetHeight(self.image_file, self.frame)

    def set_direction_no(self, dir_no):
        if self.image_file:
            D2Ext.Image_SetDirectionNo(self.image_file, dir_no)

    def get_direction_no(self):
        if self.image_file:
            return D2Ext.Image_GetDirection(self.image_file)

    def set_direction(self, direction):
        if self.image_file:
            D2Ext.Image_SetDirection(self.image_file, direction)

    def get_direction(self):
        if self.image_file:
            return D2Ext.Image_GetDirection(self.image_file)

    def draw(self):
        if not self.image_file:
            return
        D2Ext.Image_Draw(self.image_file, self.dx, self.dy + self.height, self.opacity)

    def draw_vertical_crop(self, nsize):
        if not self.image_file:
            return
        if self.parent:
            dx = self.parent.x + self.x
            dy = self.parent.y + self.y
        else:
            dx = self.x
            dy = self.y
        return D2Ext.Image_DrawVerticalCrop(self.image_file, dx, dy + self.height, 0, nsize, self.opacity)
